package com.magicsoft.share.presenter.exchange;

import com.magicsoft.share.base.BasePresenter;
import com.magicsoft.share.bean.exchange.AddressDefault;
import com.magicsoft.share.bean.exchange.Freight;
import com.magicsoft.share.bean.exchange.SubmitOrder;
import com.magicsoft.share.constant.APIService;
import com.magicsoft.share.constant.SPKey;
import com.magicsoft.share.rx_retrofit_network.location.retrofit.RetrofitUtil;
import com.magicsoft.share.rx_retrofit_network.location.rxandroid.LoadingTransformer;
import com.magicsoft.share.rx_retrofit_network.location.rxandroid.NetWorkTransformer;
import com.magicsoft.share.rx_retrofit_network.location.rxandroid.SimpleObserver;
import com.magicsoft.share.utils.SPUtils;
import com.magicsoft.share.view.exchange.ISettlementView;

import io.reactivex.annotations.NonNull;

/**
 * Created by lyf on 2017/12/4/004.
 */

public class SettlementPresenter extends BasePresenter<ISettlementView> {
    public void submitOrder(int orderAmount, String goodsList) {
        RetrofitUtil.getApi(APIService.class)
                .submitOrder(SPUtils.getInstance().getString(SPKey.TOKEN), SPUtils.getInstance().getLong(SPKey.ID), orderAmount, goodsList)
                .compose(new NetWorkTransformer<SubmitOrder>())
                .compose(new LoadingTransformer<SubmitOrder>(mContext))
                .subscribe(new SimpleObserver<SubmitOrder>(mCompositeDisposable) {
                    @Override
                    public void call(@NonNull SubmitOrder submitOrder) {

                        getView().submitOrderSuccess(submitOrder);
                    }
                });
    }

    public void freight() {
        RetrofitUtil.getApi(APIService.class)
                .freight()
                .compose(new NetWorkTransformer<Freight>())
                .compose(new LoadingTransformer<Freight>(mContext))
                .subscribe(new SimpleObserver<Freight>(mCompositeDisposable) {

                    @Override
                    public void call(@NonNull Freight freight) {
                        getView().freightSuccess(freight);
                    }


                });
    }

    public void addressDefault() {
        RetrofitUtil.getApi(APIService.class)
                .addressDefault(SPUtils.getInstance().getString(SPKey.TOKEN), SPUtils.getInstance().getLong(SPKey.ID))
                .compose(new NetWorkTransformer<AddressDefault>())
                .compose(new LoadingTransformer<AddressDefault>(mContext))
                .subscribe(new SimpleObserver<AddressDefault>(mCompositeDisposable) {

                    @Override
                    public void call(@NonNull AddressDefault aDefault) {
                        getView().addressDefaultSuccess(aDefault);
                    }


                });
    }

    public void addOrder(String userName, String phone,
                         int orderAmount, String address,
                         int freight, String goodsList,
                         String password, int isDirect,
                         long lockStockId) {
        RetrofitUtil.getApi(APIService.class)
                .addOrder(SPUtils.getInstance().getString(SPKey.TOKEN), SPUtils.getInstance().getLong(SPKey.ID),
                        userName, phone, orderAmount, address, freight, goodsList, password, isDirect, lockStockId)
                .compose(new NetWorkTransformer())
                .compose(new LoadingTransformer(mContext))
                .subscribe(new SimpleObserver(mCompositeDisposable) {
                    @Override
                    public void call(@NonNull Object o) {

                        getView().addOrderSuccess();
                    }

                    @Override
                    public void showErrorMsg(Throwable e, String errorMsg) {
                        super.showErrorMsg(e, errorMsg);
                        getView().addorderFail(errorMsg);
                    }
                });
    }
}
